﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/* LIST OF THINGS TO DO
 * 1) Add a new Injury                                          DONE--Pete
 * 2) Figure out a way to switch images to load the back        DONE--Steve
 * 3) Fix BodyParts Database                                    DONE--Pete
 * 
 * */

namespace ClinicalMassageSystem
{


    public partial class PatientProfileScreen : Form
    {
        string side = "front";
        int PatientID;
        int InjuryID;
        int facing = 1;
        bool promptSave = false;

        CMSDataContext cms = new CMSDataContext();

        public PatientProfileScreen()
        {
            InitializeComponent();
        }

        private void PatientProfileScreen_Load(object sender, EventArgs e)
        {
            loadImages("front");
            grpCurrentPalpation.Enabled = false;
            frontSide.Visible = true;
            backSide.Visible = false;
            btnSaveInjury.Enabled = false;
            btnUpdate.Enabled = false;

            if (InjuryID != 0)
                btnSaveInjury_Click(sender, e);

            lblStatus.Text = "";
        }

        private void btnSide_Click(object sender, EventArgs e)
        {
            try
            {
                if (side.Equals("front"))
                {
                    loadImages("back");
                    btnSide.Text = "Back Side";
                    side = "back";
                    facing = 2;
                    backSide.Visible = true;
                    frontSide.Visible = false;
                }
                else if (side.Equals("back"))
                {
                    loadImages("front");
                    btnSide.Text = "Front Side";
                    side = "front";
                    facing = 1;
                    backSide.Visible = false;
                    frontSide.Visible = true;
                }

                var query =
                    from ip in cms.InjuryParts
                    where ip.InjuryID == InjuryID
                    select ip.PartID;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void loadImages(string side){
            
            if (side.Equals("front"))
            {
                facing = 1;
            }
            else if (side.Equals("back"))
            {
                facing = 2;
            }

            try
            {
                var query = from ip in cms.InjuryParts
                            where (ip.InjuryID == InjuryID && ip.PatientID == PatientID)
                            select ip.PartID;

                foreach (var q in query)
                {

                    if (q == 1)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 1 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picHead.Load(i);
                    }
                    else if (q == 2)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 2 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picLArm.Load(i);
                     }
                    else if (q == 3)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 3 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picLTorso.Load(i);
                    }
                    else if (q == 4)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 4 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picRTorso.Load(i);
                    }
                    else if (q == 5)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 5 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picRArm.Load(i);
                    }
                    else if (q == 6)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 6 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picLLeg.Load(i);
                    }
                    else if (q == 7)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 7 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picRLeg.Load(i);
                    }


                    /////////////Starts the back
                    else if (q == 8)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 8 && img.DiagramID == 2)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBHead.Load(i);
                    }
                    else if (q == 9)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 9 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBLArm.Load(i);
                    }
                    else if (q == 10)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 10&& img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBLTorso.Load(i);
                    }
                    else if (q == 11)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 11 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBRTorso.Load(i);
                    }
                    else if (q == 12)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 12 && img.DiagramID == 2)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBRArm.Load(i);
                    }
                    else if (q == 13)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 13 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBLLeg.Load(i);
                    }
                    else if (q == 14)
                    {
                        var picture = from img in cms.BodyParts
                                      where (img.PartID == 14 && img.DiagramID == facing)
                                      select img.PartPath;
                        foreach (var i in picture)
                            picBRLeg.Load(i);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void loadInjury(int patID, int bodyPart)
        {
            try
            {
                injuryPartBindingSource.DataSource =
                    from painLevel in cms.InjuryParts
                    where painLevel.InjuryID == InjuryID && painLevel.PartID == bodyPart
                    select painLevel;
                bodyPartBindingSource.DataSource =
                    from p in cms.BodyParts
                    where p.PartID == bodyPart
                    select p;

                if (injuryPartBindingSource.Count == 0)
                {
                    InjuryPart newIP = (InjuryPart)injuryPartBindingSource.AddNew();
                    newIP.PatientID = PatientID;
                    newIP.PartID = bodyPart;
                    newIP.isStillInjured = true;
                    newIP.InjuryID = InjuryID;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void addInjury()
        {
            try
            {
                injuryBindingSource.DataSource =
                    from i in cms.Injuries
                    where i.PatientID == PatientID
                    select i;

                Injury newI = (Injury)injuryBindingSource.AddNew();
                newI.PatientID = PatientID;
                newI.isCurrentInjury = true;
                newI.Date = DateTime.Now.Date;
                
                grpCurrentPalpation.Enabled = false;
                frontSide.Enabled = false;
                grdPalpations.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString());
            }
        }

        private void btnSaveInjury_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtDescription.Text.Equals(""))
                    MessageBox.Show("Please enter an Injury Description");
                else
                {
                    Validate();
                    injuryBindingSource.EndEdit();
                    injuryPartBindingSource.EndEdit();
                    cms.SubmitChanges();

                    InjuryID = int.Parse(lblInjuryID.Text);

                    btnSaveInjury.Enabled = false;
                    promptSave = false;
                    lblStatus.Text = "Save Successful";

                    grdPalpations.Enabled = true;
                    //grpCurrentPalpation.Enabled = true;
                    frontSide.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message.ToString());
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Validate();
                injuryBindingSource.EndEdit();
                injuryPartBindingSource.EndEdit();
                cms.SubmitChanges();
                loadImages(side);

                btnUpdate.Enabled = false;

                //will update this later
                grdPalpations.DataSource =
                    from parts in cms.InjuryParts
                    where parts.InjuryID == InjuryID
                    select parts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void fieldChanged(object sender, EventArgs e)
        {
            this.btnSaveInjury.Enabled = true;
            this.lblStatus.Text = "Edit in progress...";
            promptSave = true;
        }

        private void promptForSave(object sender, EventArgs e)
        {
            if (promptSave)
            {
                DialogResult q = MessageBox.Show("You have not saved your current record. Would you like to save?", "Unsaved Information", MessageBoxButtons.YesNo);
                if (q == DialogResult.Yes)
                {
                    btnSaveInjury_Click(sender, e);
                    promptSave = false;
                }
            }
        }

        private void enableUpdate(object sender, EventArgs e)
        {
            btnUpdate.Enabled = true;
        }

        public void setID(string patiendID)
        {
            PatientID = int.Parse(patiendID);
            lblPatientID.Text = patiendID;
        }

        #region Set Bindings

        public void setPatientBindings(int patientID)
        {
            try
            {
                PatientID = patientID;
                patientBindingSource.DataSource =
                    from p in cms.Patients
                    where (p.PatientID == patientID)
                    select p;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void setInjuryBindings(int injuryID)
        {
            try
            {
                InjuryID = injuryID;
                injuryBindingSource.DataSource =
                    from i in cms.Injuries
                    where (i.InjuryID == injuryID)
                    select i;

                grdPalpations.DataSource =
                    from parts in cms.InjuryParts
                    where parts.InjuryID == injuryID
                    select parts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        #endregion

        #region Pic Clicks

        private void picHead_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 1);
            grpCurrentPalpation.Enabled = true;
        }

        private void picRArm_Click(object sender, EventArgs e)
        {

            loadInjury(PatientID, 5);
            grpCurrentPalpation.Enabled = true;
        }

        private void picLArm_Click(object sender, EventArgs e)
        {

            loadInjury(PatientID, 2);
            grpCurrentPalpation.Enabled = true;
        }

        private void picLLeg_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 6);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBHead_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 8);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBLArm_Click(object sender, EventArgs e)
        {

            loadInjury(PatientID, 9);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBLTorso_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 10);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBRTorso_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 12);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBLLeg_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 13);
            grpCurrentPalpation.Enabled = true;
        }

        private void picBRLeg_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 14);
            grpCurrentPalpation.Enabled = true;
        }

        private void picRLeg_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 7);
            grpCurrentPalpation.Enabled = true;
        }

        private void picRTorso_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 4);
            grpCurrentPalpation.Enabled = true;
        }

        private void picLTorso_Click(object sender, EventArgs e)
        {
            loadInjury(PatientID, 3);
            grpCurrentPalpation.Enabled = true;
        }

        #endregion

        private void picBRArm_Click(object sender, EventArgs e)
        {

        }

        private void PatientProfileScreen_FormClosed(object sender, FormClosedEventArgs e)
        {
            promptForSave(sender, e);
        }
    }
}
